<?php

/**
 * Registry management controller.
 *
 */
class Registry_Controller extends SPF_Controller {

   /**
    * The version number of the controller.
    * @var     string
    */
   const VERSION = '1.0.0';

   /**
    * The author of the controller.
    * @var     string
    */
   const AUTHOR  = 'Simon Downes';

   /**
    * Constructor
    * @return       void
    */
   public function __construct() {
      /* construct the parent object */
      parent::__construct(__CLASS__);
   }

   /**
    * Destructor
    * @access       public
    * @return       void
    */
   public function __destruct() {
   }

   /**
    * Determines if the current user is permitted to access this controller and perform the requested action
    * @return       boolean   true if access is allowed, false otherwise.
    */
   public function authenticate() {
   
      return (SPF::$session->user != NULL);
      
   } // authenticate
   
   /**
    * Lists the entries in the registry
    */
   public function index() {
      
      $this->assign('data', SPF::$registry->get_all());
      
      $this->display('browse');
      
   } // index
   
   /**
    * Creates or edits a registry entry.
    *     
    * @param   string    $var_name   The name of the variable to edit.    
    */
   public function edit( $var_name = NULL ) {
      
      $entry  = array();
      
      if( SPF::form_submitted('frm_registry') ) {
         
         // fetch posted data
         $entry['var_name']  = ($var_name === NULL) ? SPF::post('var_name') : $var_name;
         $entry['var_value'] = SPF::post('var_value');
         
         // var_name is required
         if( !$entry['var_name'] )
            $entry['errors'] = array('var_name' => SPF_Validator::ERR_MSG_REQUIRED);
         
         // if var_name wasn't supplied on the url then check if the entry already exists, so people can't replace existing entries by using the add method
         elseif( ($var_name === NULL) && SPF::$registry->exists($entry['var_name']) )
            $entry['errors'] = array('var_name' => 'Entry already exists');
               
         // if no errors then save entry and return to the browse page
         if( !$entry['errors'] ) {
            SPF::$registry->set($entry['var_name'], $entry['var_value']);
            SPF::flash(SiTrack::MSG_SAVED, 'success');
            SPF::redirect('registry', 'index');
         }
         
         SPF::flash(SiTrack::MSG_ERRORS, 'error');
         
      }
      else {
         $entry['var_name']  = $var_name;
         $entry['var_value'] = SPF::$registry->get($var_name);
      }
      
      $this->assign('entry', $entry);
      
      $this->display('edit');
      
   } // edit
   
   /**
    * Removes an entry from the registry.
    *     
    * @param   string    $var_name   The name of the entry to delete.    
    */
   public function delete( $var_name = NULL ) {
      
      if( $var_name ) {
         SPF::$registry->delete($var_name);
         SPF::flash(SiTrack::MSG_DELETED, 'success');
      }
      
      SPF::redirect('registry', 'index');
      
   } // delete
   
} // Registry_Controller

?>
